//
//  WDUploadFileRequest.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/27.
//

#import "WDUploadFileRequest.h"

@interface WDUploadFileRequest ()

@property (nonatomic, strong) NSArray<EDSImageVideoModel *> *files;

@end

@implementation WDUploadFileRequest

- (instancetype)initWithFiles:(NSArray<EDSImageVideoModel *> *)files {
    if (self = [super init]) {
        self.files = files;
    }
    return self;
}

- (NSString *)requestUrl {
    return nil;//kAPP_USER_PUT_FILE;
}

- (TYZRequestMethod)requestMethod {
    return TYZRequestMethodPOST;
}

- (id)requestArgument {
    return self.params;
}

- (AFConstructingBlock)constructingBodyBlock {
    @weakify(self);
    AFConstructingBlock block = ^(id<AFMultipartFormData> formData) {
        @strongify(self);
        for (EDSImageVideoModel *model in self.files) {
            if (model.type == 0) { // 图片
                NSData *imageData = [model.srcImage compressWithImage];
                [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.jpeg" mimeType:@"image/jpeg"];
            } else if (model.type == 1) { // 视频
                NSData *videoData = [NSData dataWithContentsOfURL:model.videoOrAudioUrl];
                // NSLog(@"videoData=%.3f", (1.0 * videoData.length) / 1024);
                
                // video/mp4
                // 原来服务器配置的MIME type为：application/octet-stream。改为video/mp4之后就好了。
                [formData appendPartWithFileData:videoData name:@"file" fileName:@"video.mp4" mimeType:@"video/mp4"];
            }
        }
    };
    return block;
}

- (Class)rspClass {
    return [WDUploadFileRspModel class];
}

@end



























